using ASP;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using IPMS.AccessAuth;
using IPMS.Sort;

public partial class WebPages : Page, IRequiresSessionState
{
    /*
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setTextReadOnly(true);
        this.setButtons(string.Empty);
    }
    */

    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            try
            {
                this.ODSPages.DeleteMethod = "Delete";
                this.ODSPages.DeleteParameters["PageName"].DefaultValue = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["PageName"].ToString();//this.txtPageName.Text.ToString();
                this.ODSPages.Delete();
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                //this.chkForOperation.Checked = true;
            }
        }
        finally
        {
            //this.btnCancel_Click(sender, e);
        }
    }
    

    /*
    protected void btnModify_Click(object sender, EventArgs e)
    {
        this.setTextReadOnly(false);
        this.setButtons("modify");
        this.txtPageName.ReadOnly = true;
        this.txtPageName.Enabled = false;
        this.txtDescription.Focus();
    }
    */

    /*
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.setTextReadOnly(false);
        this.clearText();
        this.setButtons("new");
        this.txtPageName.Enabled = true;
        this.txtPageName.ReadOnly = false;
        this.txtPageName.Focus();
    }
    */

    /*
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            try
            {
                if (this.cbForUpdate.Checked)
                {
                    this.ODSPages.UpdateMethod = "Update";
                    this.ODSPages.UpdateParameters["PageName"].DefaultValue = this.txtPageName.Text.ToString();
                    this.ODSPages.UpdateParameters["Description"].DefaultValue = this.txtDescription.Text.ToString();
                    this.ODSPages.Update();
                }
                else
                {
                    this.ODSPages.InsertMethod = "Insert";
                    this.ODSPages.InsertParameters["PageName"].DefaultValue = this.txtPageName.Text.ToString();
                    this.ODSPages.InsertParameters["Description"].DefaultValue = this.txtDescription.Text.ToString();
                    this.ODSPages.Insert();
                }
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                this.chkForOperation.Checked = true;
            }
        }
        finally
        {
            this.btnCancel_Click(sender, e);
        }
    }
    */

    /*
    protected void clearText()
    {
        this.txtPageName.Text = string.Empty;
        this.txtDescription.Text = string.Empty;
        this.cbForUpdate.Checked = false;
        this.chkForOperation.Checked = false;
        this.lblError.Text = "";
    }
    */

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        this.Sort(e, this.GridView1);
    }

    /*
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            this.chkForOperation.Checked = true;
            this.cbForUpdate.Checked = true;
            this.txtPageName.Text = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["PageName"].ToString();
            this.txtDescription.Text = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["Description"].ToString();
        }
        catch (Exception)
        {
        }
        this.setButtons(string.Empty);
    }
    */

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "WebPageMaintenance");
            if (!canView.CanView())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
            }
            else
            {
                if (!this.Page.IsPostBack)
                {
                    ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.SecurityLevels + " | " + ISitemap.WebPageMaintenance.ToString();

                    //this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
                }
                //if (!this.chkForOperation.Checked)
                //{
                //this.setButtons(string.Empty);
                //this.setTextReadOnly(true);
                //}
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
    }

    /*
    protected void setButtons(string action)
    {
        switch (action)
        {
            case "new":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                this.chkForOperation.Checked = true;
                return;

            case "modify":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                this.chkForOperation.Checked = true;
                return;

            case "save":
                this.btnNew.Enabled = true;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
                return;
        }
        this.btnNew.Enabled = true;
        this.btnModify.Enabled = this.txtPageName.Text.Length != 0;
        this.btnSave.Enabled = false;
        this.btnDelete.Enabled = this.txtPageName.Text.Length != 0;
    }
    */
    
    /*
    protected void setTextReadOnly(bool bBool)
    {
        this.txtPageName.ReadOnly = bBool;
        this.txtDescription.ReadOnly = bBool;
    }
    */

    protected void Sort(GridViewRowEventArgs e, GridView gv)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        Image image = new Image();
                        image.ImageUrl = "";
                        if (gv.SortExpression == button.CommandArgument)
                        {
                            if (gv.SortDirection == SortDirection.Ascending)
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }

    protected void btnNewWebPage_Click(object sender, EventArgs e)
    {
        base.Response.Redirect("WebPages_edit.aspx");
    }
}
